package com.yjt.sousou.detail.parts;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.yjt.sousou.R;
import com.yjt.sousou.base.BaseActivity;
import com.yjt.sousou.detail.entity.EventNum;
import com.yjt.sousou.detail.entity.PartsEntity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author chs
 * date：2019-10-25 16:14
 * des：添加配件
 */
public class AddPartsActivity extends BaseActivity {
    @BindView(R.id.tv_title)
   TextView mTvTitle;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_unit)
    TextView tvUnit;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_has)
    TextView tvHas;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_total_price)
    TextView tvTotalPrice;
    @BindView(R.id.et_num)
    EditText etNum;
    private int currentNum;
    /**
     * 库存
     */
    private int stock;
    private float price;

    @Override
    protected Object setContentLayout() {
        return R.layout.activity_add_parts;
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        mTvTitle.setText("选择配件");
        EventBus.getDefault().getStickyEvent(PartsEntity.DataEntity.class);
        setListener();
    }

    private void setListener() {
        etNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s.toString())) {
                    currentNum = Integer.parseInt(s.toString());
                    if (currentNum > stock) {
                        ToastUtils.showLong("不能超过库存");
                        currentNum = 0;
                        etNum.setText(String.valueOf(currentNum));
                    }
                    tvTotalPrice.setText(String.valueOf(currentNum*price));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onGetStickyEvent(PartsEntity.DataEntity entity) {
        tvName.setText(entity.getProname());
        tvUnit.setText(entity.getPjdw());
        tvNum.setText(entity.getXinghao());
        tvHas.setText(entity.getKucun());
        tvPrice.setText(String.format("￥%s", entity.getDanjia()));
        etNum.setText(String.valueOf(entity.getHave_num()));
        currentNum = entity.getHave_num();
        stock = Integer.parseInt(entity.getKucun());
        price = Float.parseFloat(entity.getDanjia());
        tvTotalPrice.setText(String.valueOf(currentNum*price));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.tv_add, R.id.tv_minus,R.id.btn_save})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_add:
                currentNum++;
                if (currentNum > stock) {
                    currentNum = stock;
                    ToastUtils.showLong("不能大于库存");
                }
                break;
            case R.id.tv_minus:
                currentNum--;
                if (currentNum < 0) {
                    currentNum = 0;
                }
                break;
            case R.id.btn_save:
                EventBus.getDefault().postSticky(new EventNum(currentNum));
                finish();
                break;
            default:
        }
        etNum.setText(String.valueOf(currentNum));
    }
}
